package com.tcr.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.tcr.common.TcUser;

public class UserDao {

	private UserDao() {

	}

	// insert into tc_user (u_mail,u_pass,u_name,u_type,u_upload)
	// values(?,?,?,?,?)
	@SuppressWarnings("finally")
	public static boolean addUser(String u_name, String u_mail, String u_pass,
			int u_type, int u_upload) {
		Connection conn = null;
		boolean b = false;
		try {
			conn = dbutil.pool.getConnection();
			PreparedStatement ps = conn
					.prepareStatement(SqlString.add_normal_user);

			ps.setString(1, u_name);
			ps.setString(2, u_mail);
			ps.setString(3, u_pass);
			ps.setInt(4, u_type);
			ps.setInt(5, u_upload);

			int res = ps.executeUpdate();

			b = (res != 0);
		} catch (Exception ex) {
			logger.error(ex);
		} finally {
			if (conn != null)
				dbutil.pool.backConn(conn);
			return b;
		}
	}

	// select u_name,u_type,u_upload from tc_user where u_mail = ? and u_pass =?
	@SuppressWarnings("finally")
	public static TcUser getUser(String u_mail, String u_pass) {
		Connection conn = null;
		TcUser user = null;
		try {
			conn = dbutil.pool.getConnection();
			PreparedStatement ps = conn
					.prepareStatement(SqlString.select_normal_user);
			ps.setString(1, u_mail);
			ps.setString(2, u_pass);
			ResultSet res = ps.executeQuery();
			if (res.next()) {
				int uid = (int) res.getInt(1);
				String u_name = res.getString(2);
				int u_type = (int) res.getInt(3);
				int u_upload = (int) res.getInt(4);
				user = new TcUser(uid, u_name, u_type, u_upload);
			}
		} catch (Exception ex) {
			logger.error(ex);
		} finally {
			if (conn != null)
				dbutil.pool.backConn(conn);
			return user;
		}
	}

	@SuppressWarnings("finally")
	public static boolean delUser(int uid) {
		Connection conn = null;
		boolean b = false;
		try {
			conn = dbutil.pool.getConnection();
			PreparedStatement ps = conn
					.prepareStatement(SqlString.delete_normal_user);
			ps.setInt(1, uid);

			int res = ps.executeUpdate();
			b = (res != 0);
		} catch (Exception ex) {
			logger.error(ex);
		} finally {
			if (conn != null)
				dbutil.pool.backConn(conn);
			return b;
		}
	}

	@SuppressWarnings("finally")
	public static boolean updateUserUpload(int uid, int u_upload) {
		Connection conn = null;
		boolean b = false;
		try {
			conn = dbutil.pool.getConnection();
			PreparedStatement ps = conn
					.prepareStatement(SqlString.update_normal_user_upload);
			ps.setInt(1, u_upload);
			ps.setInt(2, uid);
			int res = ps.executeUpdate();
			b = (res != 0);
		} catch (Exception ex) {
			logger.error(ex);
		} finally {
			if (conn != null)
				dbutil.pool.backConn(conn);
			return b;
		}
	}

	@SuppressWarnings("finally")
	public static List<TcUser> getAllUser() {
		Connection conn = null;
		List<TcUser> users = new ArrayList<TcUser>();
		try {
			conn = dbutil.pool.getConnection();
			Statement stmt = conn.createStatement();
			ResultSet res = stmt.executeQuery(SqlString.selec_all_users);
			while (res.next()) {
				int uid = res.getInt(1);
				String u_name = res.getString(2);
				String u_mail = res.getString(3);
				String u_pass = res.getString(4);
				int u_type = res.getInt(5);
				int u_upload = res.getInt(6);

				users.add(new TcUser(uid, u_name, u_mail, u_pass, u_type,
						u_upload));
			}
		} catch (Exception ex) {
			logger.error(ex);
		} finally {
			if (conn != null)
				dbutil.pool.backConn(conn);
			return users;
		}
	}

	@SuppressWarnings("finally")
	public static TcUser getUserById(int uid) {
		Connection conn = null;
		TcUser user = null;
		try {
			conn = dbutil.pool.getConnection();
			PreparedStatement ps = conn
					.prepareStatement(SqlString.select_user_byid);
			ps.setInt(1, uid);
			ResultSet res = ps.executeQuery();
			if (res.next()) {
				int m_uid = res.getInt(1);
				String u_name = res.getString(2);
				String u_mail = res.getString(3);
				String u_pass = res.getString(4);
				int u_type = res.getInt(5);
				int u_upload = res.getInt(6);

				user = new TcUser(m_uid, u_name, u_mail, u_pass, u_type,
						u_upload);
			}
		} catch (Exception ex) {
			logger.error(ex);
		} finally {
			if (conn != null)
				dbutil.pool.backConn(conn);
			return user;
		}
	}

	@SuppressWarnings("finally")
	public static boolean updateUserPass(int uid, String pass) {
		Connection conn = null;
		boolean b = false;
		try {
			conn = dbutil.pool.getConnection();
			PreparedStatement ps = conn
					.prepareStatement(SqlString.update_normal_user_pass);
			ps.setString(1, pass);
			ps.setInt(2, uid);
			int res = ps.executeUpdate();
			b = (res != 0);
		} catch (Exception ex) {
			logger.error(ex);
		} finally {
			if (conn != null)
				dbutil.pool.backConn(conn);
			return b;
		}
	}

	private static DbUtil dbutil = DbUtil.getInstance();
	private final static Logger logger = Logger.getLogger(UserDao.class);
}
